import Util from '../../utils/util.js';
import {updateTag} from "../../api/member";
import {CACHE_USERINFO} from "../../config";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    tags: [

      {title: "就诊人员", id: 1,},
      {title: "在职职工", id: 2,},
      // {title: "中南游客", id: 6,},
      {title: "其他", id: 5,},

    ],
    btnActive: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideHomeButton()

    const that = this;
    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo,
      })
      wx.hideLoading()
    }).then(() => {
      this.onLoadTabbar();
    })

  },

  selectTag(e) {
    const that = this,
      tagList = this.data.tags,
      key = e.currentTarget.dataset.key;

    if (!this.data.btnActive)
      return;

    wx.showLoading({
      title: "正在保存..."
    })
    updateTag(tagList[key].id).then((res) => {

      let userInfo = that.data.userInfo || [];
      userInfo.tags = res.data.tags || null;
      app.globalData.userInfo = userInfo;

      wx.setStorageSync(CACHE_USERINFO, JSON.stringify(userInfo));
      Util.Tips({title: "保存成功",}, {tab: 1, url: "/pages/index/index"})
    }).finally(() => {
      that.setData({
        btnActive: true,
      })
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      wx.createSelectorQuery().in(that).select('.weui-footer')
        .boundingClientRect(function (rect) {
          if (!rect)
            return;
          that.setData({
            bottom: parseInt(rect.height || 0) + 15
          })
        }).exec()

      that.setData({isIphoneX: res})
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})